﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxTreeList;
using Utility1;

namespace Thv.admin.Pages
{
    public partial class sodo : System.Web.UI.UserControl
    {
        protected string template_path;
        protected void Page_Load(object sender, EventArgs e)
        {
            template_path = WebUtils.SystemUrl();
            try
            {
                Bindata();
                if (!IsPostBack)
                {
                    getListUser();
                    getListTV();
                    getListCTV();
                    binusername();
                }
            }
            catch (Exception) { }
        }
        void binusername()
        {
            try
            {
                DataClasses1DataContext dx = new DataClasses1DataContext();
                var list = from k in dx.UserDBs
                           where k.UserName == Session["Uss"].ToString()
                           select k;
                foreach (UserDB u in list)
                    lbFullname.Text = "Hệ thống của: "+u.Lastname + " " + u.FirtName +" ("+u.capbac+")";
            }
            catch (Exception) { }
        }
        void Bindata()
        {
            try
            {
                if (txtUser.Text == "")
                {
                    string id = Request.QueryString["Id"].ToString();
                    Session["IdUse"] = id;
                }
            }
            catch (Exception) { }
            //
            try
            {
                DataClasses1DataContext dx = new DataClasses1DataContext();
                var list = from k in dx.UserDBs
                           where k.kichhoat == true && k.flag == 1
                           select k;
                treelist.DataSource = list;
                treelist.RootValue = int.Parse(Session["IdUse"].ToString());
                treelist.RootNode.Expanded = true;
                treelist.DataBind();
            }
            catch (Exception) { }
        }
        
        void getListUser()
        {
            try
            {
                string dteForm = DateTime.Parse("01/10/2012").ToString("MM/dd/yyyy");
                string dteTo = DateTime.Now.ToString("MM/dd/yyyy");
                if (txtDateFrom.Text != "")
                    dteForm = txtDateFrom.Date.ToString("MM/dd/yyyy");
                if (txtDateTo.Text != "")
                    dteTo = txtDateTo.Date.ToString("MM/dd/yyyy");

                string Sql = string.Format("select * from  uf_Thongkesonguoi({0},{1},{2},{3})", Session["IdUse"].ToString(), "'" + dteForm + "'", "'" + dteTo + "'", 14);
                Xuly xl = new Xuly();
                grvAll.DataSource = xl.ExcuteSQL_Table(Sql).DefaultView;
                grvAll.DataBind();
            }
            catch (Exception) { }
        }

        void getListTV()
        {
            try{
            string ngaycuoi = "30";
            string thangdau = "1";
            string thang = "1";
            int ncuoi = DateTime.Today.Day;
            int thangnay = DateTime.Today.Month;
            int thangtruoc=thangnay-1;
            //kiem tra thang nay neu la 1 thi xet thang truoc la 12
            if(thangnay==1)
                thangtruoc=12;
            // neu thang truoc ma nho hon 10 thi cong them so khong nguoc lai thi = thang truoc 
            if (thangtruoc < 10)
                thangdau = "0" + thangtruoc;
            else
                thangdau = thangtruoc.ToString();
            // neu ngay cuoi thang ma nho hon 10 thi cộng them so 0
            if (ncuoi < 10)
                ngaycuoi = "0" + ncuoi;
            //neu thang truoc ma bang 2 thi xet ngay cuoi cung cua thang do =28
            if (thangtruoc == 2)
                ngaycuoi = "28";
            else 
                ngaycuoi = "30";
            // neu thang nay ma nho hon 10 thi + them 0
            if (thangnay < 10)
                thang = "0" + thangnay;
            else
                thang = thangnay.ToString();
     

            int nam = DateTime.Now.Year;
            if (thangnay == 1)
                nam = nam - 1;

            string dteForm = DateTime.Parse("01/10/2012").ToString("MM/dd/yyyy");
            string dteTo = thangdau +"/"+ngaycuoi+"/"+ nam;

            string dteForm2 = thang + "/" + "01" +"/"+ DateTime.Now.Year;
            string dteTo2 = DateTime.Now.ToString("MM/dd/yyyy");

            if (txtDateFrom.Text != "")
                dteForm2 = txtDateFrom.Date.ToString("MM/dd/yyyy");
            if (txtDateTo.Text != "")
                dteTo2 = txtDateTo.Date.ToString("MM/dd/yyyy");
            int index=1;
               
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.UserDBs
                       where k.ID == int.Parse(Session["IdUse"].ToString())
                       select k;
            foreach (UserDB u in list)
                if (u.capbac == "Cộng Tác Viên")
                    index = 2;
            string Sql = string.Format("select * from  uf_Thongke({0},{1},{2},{3},{4},{5},{6})", Session["IdUse"].ToString(), "'" + dteForm + "'", "'" + dteTo + "'", "'" + dteForm2 + "'", "'" + dteTo2 + "'", 14, index);
            Xuly xl = new Xuly();
            grvTV.DataSource = xl.ExcuteSQL_Table(Sql).DefaultView;
            grvTV.DataBind();
            }
            catch (Exception) { }
        }

        void getListCTV()
        {
            try{
            string ngaycuoi = "30";
            string thangdau = "1";
            string thang = "1";
            int ncuoi = DateTime.Today.Day;
            int thangnay = DateTime.Today.Month;
            int thangtruoc = thangnay - 1;
            //kiem tra thang nay neu la 1 thi xet thang truoc la 12
            if (thangnay == 1)
                thangtruoc = 12;
            // neu thang truoc ma nho hon 10 thi cong them so khong nguoc lai thi = thang truoc 
            if (thangtruoc < 10)
                thangdau = "0" + thangtruoc;
            else
                thangdau = thangtruoc.ToString();
            // neu ngay cuoi thang ma nho hon 10 thi cộng them so 0
            if (ncuoi < 10)
                ngaycuoi = "0" + ncuoi;
            //neu thang truoc ma bang 2 thi xet ngay cuoi cung cua thang do =28
            if (thangtruoc == 2)
                ngaycuoi = "28";
            else
                ngaycuoi = "30";
            // neu thang nay ma nho hon 10 thi + them 0
            if (thangnay < 10)
                thang = "0" + thangnay;
            else
                thang = thangnay.ToString();


            int nam = DateTime.Now.Year;
            if (thangnay == 1)
                nam = nam - 1;

            string dteForm = DateTime.Parse("01/10/2012").ToString("MM/dd/yyyy");
            string dteTo = thangdau + "/" + ngaycuoi + "/" + nam;

            string dteForm2 = thang + "/" + "01" + "/" + DateTime.Now.Year;
            string dteTo2 = DateTime.Now.ToString("MM/dd/yyyy");

            if (txtDateFrom.Text != "")
                dteForm2 = txtDateFrom.Date.ToString("MM/dd/yyyy");
            if (txtDateTo.Text != "")
                dteTo2 = txtDateTo.Date.ToString("MM/dd/yyyy");

            string Sql = string.Format("select * from  uf_Thongke_CTV({0},{1},{2},{3},{4},{5})", Session["IdUse"].ToString(), "'" + dteForm + "'", "'" + dteTo + "'", "'" + dteForm2 + "'", "'" + dteTo2 + "'", 14);
            Xuly xl = new Xuly();
            grvCTV.DataSource = xl.ExcuteSQL_Table(Sql).DefaultView;
            grvCTV.DataBind();
            }
            catch (Exception) { }
        }

        protected void cbPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            switch (e.Parameter)
            {
                case "ChangeLoai":
                    DataClasses1DataContext dx = new DataClasses1DataContext();
                    var Listli = from k in dx.UserDBs
                                 where k.UserName == txtUser.Text.Trim()
                                 select k;
                    foreach (UserDB u in Listli)
                    {
                        Session["IdUse"] = u.ID;
                        lbFullname.Text = "Hệ thống của: " + u.Lastname + " " + u.FirtName + " (" + u.capbac + ")"; ;
                    }
                    
                    Bindata();
                    break;
                case "Timkiem":
                    getListUser();
                    getListTV();
                    getListCTV();
                    break;
                case "thaydoi":

                    TreeListDataColumn col = (TreeListDataColumn)treelist.Columns["UserName"];
                   // HiddenField hdf = (HiddenField)treelist.FindControl("hdf");
                    HiddenField ddl = (HiddenField)treelist.FindDataCellTemplateControl("Id", col, "hdf");
                    
                    break;
            }
        }

        #region Expord

        //sơ đồ
        protected void btnPdfExport_Click(object sender, EventArgs e)
        {
            PrepareExporter();
            treeListExporter.WritePdfToResponse();
          }
        protected void btnXlsxExport_Click(object sender, EventArgs e)
        {
            PrepareExporter();
            treeListExporter.WriteXlsxToResponse();
        }
        protected void btnRtfExport_Click(object sender, EventArgs e)
        {
            PrepareExporter();
            treeListExporter.WriteRtfToResponse();
        }
        void PrepareExporter() {
            treeListExporter.Settings.AutoWidth = true;
            treeListExporter.Settings.ExpandAllNodes = true;
            treeListExporter.Settings.ShowTreeButtons = true;
            treeListExporter.Settings.ShowTreeLines = true;
            treelist.Settings.ShowTreeLines = true;
            treelist.Settings.ShowRoot = true;
            treeListExporter.TreeList.Settings.ShowTreeLines=true;
            treeListExporter.TreeList.SettingsBehavior.AllowFocusedNode = false;
            treeListExporter.Styles.Default.BorderSides = 0;
            treeListExporter.Styles.Default.BorderWidth = 0;
            treeListExporter.Styles.Cell.BorderWidth = 0;
        }

        //Bảng 1
        protected void btnPdfExport1_Click(object sender, EventArgs e)
        {
            grvExportTv.WritePdfToResponse();
        }
        protected void btnXlsxExport1_Click(object sender, EventArgs e)
        {
            grvExportTv.WriteXlsxToResponse();
        }
        protected void btnRtfExport1_Click(object sender, EventArgs e)
        {
            grvExportTv.WriteRtfToResponse();
        }

        //Bảng 2

        protected void btnPdfExport2_Click(object sender, EventArgs e)
        {
            grvExportCTv.WritePdfToResponse();
        }
        protected void btnXlsxExport2_Click(object sender, EventArgs e)
        {
            grvExportCTv.WriteXlsxToResponse();
        }
        protected void btnRtfExport2_Click(object sender, EventArgs e)
        {
            grvExportCTv.WriteRtfToResponse();
        }

        // Bảng 3
        protected void btnPdfExport3_Click(object sender, EventArgs e)
        {
            grvExportAll.WritePdfToResponse();
        }
        protected void btnXlsxExport3_Click(object sender, EventArgs e)
        {
            grvExportAll.WriteXlsxToResponse();
        }
        protected void btnRtfExport3_Click(object sender, EventArgs e)
        {
            grvExportAll.WriteRtfToResponse();
        }

        
        #endregion

        protected void grvTV_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            getListTV();
        }

        protected void grvCTV_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            getListCTV();
        }

        protected void grvAll_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            getListUser();
        }

        protected void grvTV_Load(object sender, EventArgs e)
        {
            getListTV();
        }

        protected void grvCTV_Load(object sender, EventArgs e)
        {
            getListCTV();
        }

        protected void grvAll_Load(object sender, EventArgs e)
        {
            getListUser();
        }
    }
}